home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / mac_io.c < prev    next >
C/C++ Source or Header  |  1992-02-11  |  20KB  |  1,144 lines

  1. /* OS- and machine-dependent stuff for Mac  */
  2.  
  3. /*mac_io.c
  4.  *    Routines for Macintosh IO and file stuff
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "internet.h"
  10. #include "iface.h"
  11. #include "mac.h"
  12. #include "cmdparse.h"
  13.  
  14. #include "DeviceMgr.h"
  15. #include "WindowMgr.h"
  16. #include "EventMgr.h"
  17. #include "SerialDvr.h"
  18. #include "HFS.h"
  19. #include <time.h>
  20. extern long timezone;
  21. extern errno;
  22.  
  23. struct Store_input Store_input[ASY_MAX];
  24.  
  25. struct asy asy[ASY_MAX];
  26.  
  27. static ioParam    MacSer;
  28.  
  29. struct interface *ifaces;
  30. static struct RemoveIt {
  31.     struct RemoveIt *next;
  32.     char *name_ptr;
  33. } *Head;
  34.  
  35.  
  36. /* Called at startup time to set up console I/O, memory heap */
  37. ioinit()
  38. {
  39.     timezone = 60*60+1;
  40.     tzname[0] = "CST";
  41.     tzname[1] = "CST";
  42.     Click_On(0);
  43.     Head = malloc(sizeof (struct RemoveIt));
  44.     Head->next = NULL;
  45.     Head->name_ptr = NULL;
  46.     return(0);
  47. }
  48. /* Called just before exiting to restore console state */
  49. iostop()
  50. {
  51.     int i;
  52.     struct RemoveIt *rptr;
  53.     
  54.     while(ifaces != NULLIF){
  55.         if(ifaces->stop != NULLFP)
  56.             (*ifaces->stop)(ifaces);
  57.         ifaces = ifaces->next;
  58.     }
  59.     /*
  60.      * I want to close down all the files and then remove the (possibly still existing) files
  61.      * because the MAC will not allow the file to be removed when it still has an open file
  62.      * descriptor
  63.      */
  64.      
  65.     for( i = 3; i < _NFILE; i++)
  66.     {
  67.         close(i);
  68.     }
  69.     (void)unlink("dir.temp");
  70.     rptr = Head;
  71.     while( rptr->next != NULL)
  72.     {
  73.         if ( rptr->name_ptr != NULL)
  74.             unlink(rptr->name_ptr);
  75.         rptr = rptr->next;
  76.     }    
  77. }
  78.  
  79. /* Initialize asynch port "dev" */
  80.  
  81. int slipisopen = 0;
  82. int iref;
  83. int oref;
  84.  
  85. char Recv_buf[MAX_STORE];
  86. char Send_buf[MAX_STORE];
  87.  
  88. int
  89. asy_init(dev,arg1,arg2,bufsize)
  90. int16 dev;
  91. char *arg1,*arg2;
  92. unsigned bufsize;
  93. {
  94.     register struct asy *ap;
  95.     register struct interface *if_asy;
  96.     extern struct interface *ifaces;
  97.     char ser_name[255];
  98.     struct Store_input *store;
  99.     
  100.     OsErr e;
  101.     ap = &asy[dev];
  102.  
  103.     ap->tty = malloc(strlen(arg2)+1);
  104.     strcpy(ap->tty, arg2);
  105.  
  106. #ifdef DEBUG
  107.     printf("asy_init: as->tty = %s, dev = %d\n", ap->tty, dev); 
  108. #endif
  109.     if ( ap == NULL)
  110.     {
  111.         return(-1);
  112.     }
  113.     
  114.     switch ( ap->tty[0] )
  115.     {
  116.         case 'a':
  117.         case 'A':
  118.             if ( ap->devopen == 0 )
  119.             {
  120.                 e = RAMSDOpen(sPortA);
  121.                 if ( e != noErr )
  122.                 {
  123.                     printf("RAMSDOpen failed, e = %d\n",e);
  124.                     SysBeep(4);
  125.                     ExitToShell();
  126.                 }
  127.                 e = OpenDriver ("\p.AIn", &iref);
  128.                 if ( e != noErr) {
  129.                     printf("OpenDriver for AIn failed, e = %d\n",e);
  130.                     SysBeep (4);
  131.                     ExitToShell ();
  132.                 }
  133.                 e = OpenDriver ("\p.AOut", &oref);
  134.                 if ( e != noErr) {
  135.                     printf("OpenDriver for AOut failed, e = %d\n",e);
  136.                     SysBeep (4);
  137.                     CloseDriver (iref);
  138.                     ExitToShell ();
  139.                 }
  140.                 ap->portIn = AinRefNum;
  141.                 ap->portOut = AoutRefNum;
  142.                 ap->devopen = 1;
  143.             }
  144.             else
  145.             {
  146.                 printf("Device %c is already open.\n", ap->tty[0]);
  147.                 return(-1);
  148.             }
  149.             break;
  150.         
  151.         case 'b':
  152.         case 'B':
  153.             if ( ap->devopen == 0)
  154.             {
  155.                 e = RAMSDOpen(sPortB);
  156.                 if ( e != noErr )
  157.                 {
  158.                     printf("RAMSDOpen failed, e = %d\n", e);
  159.                     SysBeep(4);
  160.                     ExitToShell();
  161.                 }
  162.                 e = OpenDriver ("\p.BIn", &iref);
  163.                 if ( e != noErr) {
  164.                     printf("OpenDriver for BIn failed, e = %d\n", e);
  165.                     SysBeep (4);
  166.                     ExitToShell ();
  167.                 }
  168.                 e = OpenDriver ("\p.BOut", &oref);
  169.                 if ( e != noErr) {
  170.                     printf("Opendriver for Bout failed e = %d\n",e);
  171.                     SysBeep (4);
  172.                         CloseDriver (iref);
  173.                     ExitToShell ();
  174.                 }
  175.                 ap->portIn = BinRefNum;
  176.                 ap->portOut = BoutRefNum;
  177.                 ap->devopen = 1;
  178.             }
  179.             else
  180.             {
  181.                 printf("Device %c is already open.\n", ap->tty[0] );
  182.                 return(-1);
  183.             }
  184.             break;
  185.         
  186.         default:
  187.             printf("Unknown device %c, could not configure port,\n", ap->tty);
  188.             return(-1);
  189.     }
  190.         
  191.         
  192.     store = &Store_input[dev];
  193.     store->head =  store->store;
  194.     store->tail = store->store;
  195.     store->amt = 0;
  196.     e = SerSetBuf( ap->portIn, Recv_buf, MAX_STORE);
  197.     if ( e != noErr) {
  198.         printf("asy_init: SerSetBuf error on %d\n", ap->portIn);
  199.         SysBeep (4);
  200.         CloseDriver (iref);
  201.         ExitToShell ();
  202.     }
  203.     SerSetBuf( ap->portOut, Send_buf, MAX_STORE);
  204.     if ( e != noErr) {
  205.         printf("asy_init: SerSetBuf error on %d\n", ap->portOut);
  206.         SysBeep (4);
  207.         CloseDriver (iref);
  208.         ExitToShell ();
  209.     }
  210.     slipisopen = 1;
  211.     return (0);
  212. }
  213.  
  214. int
  215. asy_stop(interface)
  216. struct interface *interface;
  217. {
  218.     register struct asy *ap;
  219.     int ref;
  220.     
  221.     ap = &asy[interface->dev];
  222.  
  223.     if (slipisopen)
  224.     {
  225.         MacSer.ioActCount = 0;
  226.         MacSer.ioRefNum = ap->portIn;
  227.         MacSer.ioCompletion = 0;
  228.         MacSer.ioBuffer = ap->recv_buf;
  229.         MacSer.ioReqCount = 0;
  230.         MacSer.ioPosMode = 1;
  231.         (void) PBKillIO(&MacSer, FALSE);
  232.         
  233.         MacSer.ioActCount = 0;
  234.         MacSer.ioRefNum = ap->portOut;
  235.         MacSer.ioCompletion = 0;
  236.         MacSer.ioBuffer = ap->recv_buf;
  237.         MacSer.ioReqCount = 0;
  238.         MacSer.ioPosMode = 1;
  239.         (void) PBKillIO(&MacSer, FALSE);
  240.  
  241.         FSClose(ap->portIn);
  242.         FSClose(ap->portOut);
  243.         if ( ap->portIn = AinRefNum)
  244.         {
  245.             OpenDriver("\p.Ain", &ref);
  246.             OpenDriver("\p.Aout", &ref);
  247.         }
  248.         else
  249.         {
  250.             OpenDriver("\p.Bin", &ref);
  251.             OpenDriver("\p.Bout", &ref);
  252.         }
  253.         
  254.         slipisopen = 0;
  255.     }
  256. }
  257.  
  258. /* Asynchronous line I/O control */
  259. asy_ioctl(interface,argc,argv)
  260. struct interface *interface;
  261. int argc;
  262. char *argv[];
  263. {
  264.     if(argc < 1){
  265.         printf("%d\r\n",asy[interface->dev].speed);
  266.         return 0;
  267.     }
  268.     return asy_speed(interface->dev,atoi(argv[0]));
  269. }
  270.  
  271.  
  272. /* Set asynch line speed */
  273. int
  274. asy_speed(dev,speed)
  275. int dev;
  276. int speed;
  277. {
  278.     OsErr e;
  279.     int serialconfig;
  280.     SerShk    HandShake;
  281.     struct asy *ap;
  282.  
  283.     
  284.     ap = &asy[dev];
  285.  
  286.     if(speed == 0 || dev >= nasy)
  287.         return(-1);
  288.     
  289. #ifdef DEBUG
  290.     printf("asy_speed: Setting speed for device %d to %d\n",dev, speed);
  291. #endif
  292.     asy[dev].speed = speed;
  293.  
  294.     switch(speed)
  295.     {
  296.         case 300:
  297.             serialconfig = baud300;
  298.             break;
  299.         case 600:
  300.             serialconfig = baud600;
  301.             break;
  302.         case 1200:
  303.             serialconfig = baud1200;
  304.             break;
  305.         case 1800:
  306.             serialconfig = baud1800;
  307.             break;
  308.         case 2400:
  309.             serialconfig = baud2400;
  310.             break;
  311.         case 3600:
  312.             serialconfig = baud3600;
  313.             break;
  314.         case 4800:
  315.             serialconfig = baud4800;
  316.             break;
  317.         case 7200:
  318.             serialconfig = baud7200;
  319.             break;
  320.         case 9600:
  321.             serialconfig = baud9600;
  322.             break;
  323.         case 19200:
  324.             serialconfig = baud19200;
  325.             break;
  326.         case (long)57600:
  327.             serialconfig = baud57600;
  328.             break;
  329.         default:
  330.             printf("asy_speed: Unknown speed (%ld)\n", speed);
  331.             break;
  332.     }
  333. /*    
  334.     printf("serialconfig = %d\n", serialconfig);
  335. */
  336.     serialconfig |= (stop10|noParity|data8);
  337.  
  338.  
  339.     e = SerReset( ap->portIn, serialconfig);
  340.     if ( e != noErr) {
  341.         printf("asy_speed: could not set config for %d\n", ap->portIn);
  342.         SysBeep (4);
  343.         CloseDriver (iref);
  344.         ExitToShell ();
  345.     }
  346.     
  347.     bzero(&HandShake, sizeof (SerShk));
  348.     e = SerHShake(ap->portIn, &HandShake);
  349.     if ( e != noErr) {
  350.         printf("asy_speed: could not set handshake for %d\n", ap->portIn);
  351.         SysBeep (4);
  352.         CloseDriver (iref);
  353.         ExitToShell ();
  354.     }
  355.     
  356.     e = SerReset( ap->portOut, serialconfig);
  357.     if ( e != noErr) {
  358.         printf("asy_speed: could not set config for %d\n", ap->portOut);
  359.         SysBeep (4);
  360.         ExitToShell ();
  361.     }
  362.     bzero(&HandShake, sizeof (SerShk));
  363.     e = SerHShake(ap->portOut, &HandShake);
  364.     if ( e != noErr) {
  365.         printf("asy_speed: could not set handshake for %d\n", ap->portOut);
  366.         SysBeep (4);
  367.         CloseDriver (iref);
  368.         ExitToShell ();
  369.     }
  370.     /*
  371.     printf("asy_speed: completed.\n");
  372.     */
  373.     return(0);
  374.  
  375. }
  376. /* Send a buffer to serial transmitter */
  377. asy_output(dev,buf,cnt)
  378. unsigned dev;
  379. char *buf;
  380. unsigned short cnt;
  381. {
  382.     register struct asy *ap;
  383.     long amount = (long)cnt;
  384.     ap = &asy[dev];
  385.     /*
  386.     printf("asy_output called. dev = %x, cnt = %d\n", dev, cnt);
  387.     */
  388.     if(dev >= nasy)
  389.         return(-1);
  390.     FSWrite(ap->portOut,&amount, buf);
  391.     return(0);
  392.  
  393. }
  394.  
  395. /*
  396.  * Receive characters from asynch line
  397.  * Returns count of characters read
  398.  */
  399.  
  400. unsigned
  401. asy_recv(dev,buf,cnt)
  402. int dev;
  403. char *buf;
  404. unsigned cnt;
  405. {
  406.     long amount[8];
  407.     int tot = 0;
  408.     struct Store_input *store;
  409.     char store_it[MAX_STORE];
  410.     int got = 0;
  411.     int tt;
  412.      struct asy *ap;
  413.  
  414. #ifdef DEBUG
  415. printf("asy_recv: dev = %d\n", dev);    
  416. #endif
  417.  
  418.     if ( dev > nasy )
  419.     {
  420.         printf("Error on asy_recv. dev = %d, max = %d\n", dev, nasy);
  421.         return(-1);
  422.     }
  423.  
  424.     ap = &asy[dev];
  425.  
  426.     store = &Store_input[dev];        /* point to current */
  427.  
  428. /* printf("asy_recv: want %d, buffer has %d bytes\n", cnt, store->amt);    */
  429.  
  430.     if ( cnt > 0 && store->amt > 0)
  431.     {
  432.         tt = min(cnt , store->amt);
  433.         tt = min(tt,    MA